package com.bwie.rabbit;

import cn.hutool.core.bean.BeanUtil;
import com.bwie.domain.User;
import com.bwie.domain.UserGroup;
import com.bwie.mapper.UserGroupMapper;
import com.bwie.server.UserServer;
import com.bwie.utils.R;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * TODO
 *
 * @Description
 * @Author 36167
 * @Date 2025/3/19 15:44
 **/


@Component
public class Rabbit {

    @Autowired
    UserServer userServer;

    @Autowired
    JavaMailSender javaMailSender;

    @RabbitListener(queues = "day3-19")
    public void a1(String msg, Message message, Channel channel){
        String s = new String(msg);
        R getuser = userServer.getuser(Integer.valueOf(s));
        User bean = BeanUtil.toBean(getuser.getData(), User.class);

        SimpleMailMessage message1 = new SimpleMailMessage();
       message1.setTo(bean.getMailbox());
       message1.setSubject("开团成功");
       message1.setText("用户开团成功");
       message1.setFrom("3616778020@qq.com");
       javaMailSender.send(message1);
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RabbitListener(queues = "xd")
    public void a2(String msg, Message message, Channel channel){
        String s = new String(msg);
        R getuser = userServer.getuser(Integer.valueOf(s));
        User bean = BeanUtil.toBean(getuser.getData(), User.class);

        SimpleMailMessage message1 = new SimpleMailMessage();
        message1.setTo(bean.getMailbox());
        message1.setSubject("参团成功");
        message1.setText("用户参团成功");
        message1.setFrom("3616778020@qq.com");
        javaMailSender.send(message1);
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = "tk")
    public void a3(String msg, Message message, Channel channel){
        String s = new String(msg);
        R getuser = userServer.getuser(Integer.valueOf(s));
        User bean = BeanUtil.toBean(getuser.getData(), User.class);

        SimpleMailMessage message1 = new SimpleMailMessage();
        message1.setTo(bean.getMailbox());
        message1.setSubject("退款成功");
        message1.setText("用户退款成功");
        message1.setFrom("3616778020@qq.com");
        javaMailSender.send(message1);
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
